package com.alks.function.controller.produce;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.StringUtils;
import com.alks.entity.data.entity.dispatch.LotHead;
import com.alks.entity.data.entity.dispatch.ManulDispatchDetail;
import com.alks.entity.data.entity.dispatch.ManulDispatchHead;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.mapper.produce.CuttingDispatchHeadMapper;
import com.alks.function.service.produce.CuttingDispatchHeadService;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author:hxl
 * @create: 2024-07-05 16:28
 * @Description:
 */
@ApiRestController("/plan/dispatch")
@Api(tags = "手工冲裁")
public class ManulDispatchController {
    @Autowired
    private CuttingDispatchHeadService cuttingDispatchHeadService;

    //手工派工查询
    @PostMapping({"/list"})
    @ApiOperation(value = "手工冲裁查询", tags = "手工冲裁")
    @WebLogger
    public ResponseInfo searchList(ManulDispatchHead manulDispatchHead) {
        return cuttingDispatchHeadService.searchList(manulDispatchHead);
    }

    @PostMapping({"/detail/list"})
    @WebLogger
    @ApiOperation(value = "派工号码明细", tags = "手工冲裁")
    public ResponseInfo dispatchDetailList(@RequestBody ManulDispatchHead manulDispatchHead) {
        manulDispatchHead.setProcessCode("01");
        return cuttingDispatchHeadService.dispatchDetailSizeList(manulDispatchHead);
    }

    //删除
    @PostMapping({"/delete"})
    @WebLogger
    @ApiOperation(value = "删除",tags = "手工冲裁")
    public ResponseInfo delete(@RequestBody List<ManulDispatchHead> list){
        return cuttingDispatchHeadService.delete(list);
    }

    @PostMapping("/print")
    @WebLogger
    @ApiOperation(value = "打印派工单返回数据",tags = "手工冲裁")
    public ResponseInfo planNoPrint(@RequestParam String dispatchType, HttpServletRequest request) throws IOException {
        return cuttingDispatchHeadService.planNoPrint(dispatchType,request);
    }

    //更新打印状态
    @PostMapping("/updatePrintStatus")
    @WebLogger
    @ApiOperation(value = "更新打印状态",tags = "手工冲裁")
    public ResponseInfo updatePrintStatus(String dispatchNo){
        cuttingDispatchHeadService.updatePrintStatus(dispatchNo);
        return ResponseInfo.ok();
    }

    @PostMapping({"/lotNo/page/combo"})
    @WebLogger
    @ApiOperation(value = "选择指令返回",tags = "手工冲裁")
    public ResponseInfo returnLotNo(String searchKey) {
        return cuttingDispatchHeadService.returnLotNo(searchKey);
    }

    @PostMapping({"/lsNo/list"})
    @WebLogger
    @ApiOperation(value = "轮次和工艺返回",tags = "手工冲裁")
    public ResponseInfo returnRoundAndPro(@RequestParam String lotNo) {
        return cuttingDispatchHeadService.returnRoundAndPro(lotNo);
    }

    @PostMapping({"/tree/list"})
    @WebLogger
    @ApiOperation(value = "部位/号码明细",tags = "手工冲裁")
    public ResponseInfo loadManulLsTress(@RequestParam String lotNo, @RequestParam List<String> lsNos, @RequestParam List<String> process){
        return cuttingDispatchHeadService.loadCutLabelTrees(lotNo,lsNos,process);
        //["裁剪"] [1]
    }

    //
    @PostMapping({"/worker/list"})
    @WebLogger
    @ApiOperation(value = "当日手工员工信息",tags = "手工冲裁")
    public  List<Map<String, Object>> loadManulWorkerList(@RequestParam @JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8") @DateTimeFormat(pattern = "yyyy-MM-dd") Date dispatchDate, @RequestParam(required = false) String searchKey){
        return cuttingDispatchHeadService.loadManulWorkerList(dispatchDate,searchKey);
    }

    //插入派工
    @PostMapping({"/sg/doDispatch"})
    @WebLogger
    @ApiOperation(value = "插入派工",tags = "手工冲裁")
    public ResponseInfo insertIntoDispatch(@RequestBody List<ManulDispatchDetail> details,@RequestParam String lotNo, @RequestParam @JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8") @DateTimeFormat(pattern = "yyyy-MM-dd") Date dispatchDate, @RequestParam String manulWorkerId){
        return cuttingDispatchHeadService.insertIntoDispatch(details,lotNo,dispatchDate,manulWorkerId);
    }

    //打印汇总表
    @PostMapping({"/summary/print"})
    @WebLogger
    @ApiOperation(value = "打印汇总表",tags = "手工冲裁")
    public ResponseInfo summaryPrintTotal(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") Date beginDate, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") Date endDate){
        return cuttingDispatchHeadService.summaryPrintTotal(beginDate,endDate);
    }

    @Autowired
    CuttingDispatchHeadMapper mapper;

    @PostMapping({"/sizeNoCombo"})
    @ApiOperation(value = "号码",tags = "手工冲裁")
    public List<Map<String, Object>> loadManulLsSizeNoCombo(@RequestParam String lotNo, @RequestParam List<String> lsNos, @RequestParam List<String> process) {
        LotHead lotHead = new LotHead();
        lotHead.setLotNo(lotNo);
        lotHead.setCompanyId(UserIdThread.get().getCompanyId());
        List<Map<String, Object>> result = mapper.getManulLsSizeNo(lotHead, lsNos, process);
        return result;
    }
}
